package com.chandra.pos.common.web;

import com.chandra.pos.common.model.POSConstant;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * Created by Admin on 7/2/14.
 */
public class POSController {

    protected static String PAGE_HOME = "home";
    protected static String PAGE_NEW = "new";
    private int tabIndex;


    protected static String PAGE_REDIRECT = "redirect:";


    protected String getPageView(String pageModule, String pageName) {

        return pageModule + POSConstant.PAGE_SEPERATOR + pageName;
    }

    protected String getPageURL(String pageModule, String pageName) {

        return PAGE_REDIRECT + pageName;
    }


    @ModelAttribute("currentUser")
       public UserDetails getLoggedInUser() {

        UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return user;
    }


    @ModelAttribute("applicationDate")
    public String getApplicationDate() {

        DateTimeFormatter dateTimeFormatter= DateTimeFormat.forPattern(POSConstant.APP_DATE_FORMAT);


        return dateTimeFormatter.print(DateTime.now());
    }


    @ModelAttribute("tabIndex")
    public int getTabIndex() {
        return tabIndex;
    }

    public void setTabIndex(int tabIndex) {
        this.tabIndex = tabIndex;
    }


}
